package com.business.user.controller;
import com.business.user.validator.UserValidator;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.common.util.md5.MD5Util;
import com.common.util.picture.PictureBase64;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.model.User;

public class UserController extends Controller {

	// 注册用户
	public void register() {
			User user = new User();
			user.setUserName(getPara("userName"));
			user.setUserPassword(getPara("password"));
			user.setEmail(getPara("email"));
			user.setUserPassword(MD5Util.MD5(user.getUserPassword()));
			user.setCreateTime(new Date());
			user.setIntegral(10d);
			user.setUserLevel(0);
			user.setLoginTime(new Date());
			user.save();
			getSession().setAttribute("user", user);
			//redirect是重定向方法
			redirect("/");
	}

	// 登录验证
	public void checklogin() {
		User user = User.dao.findFirst("select * from user where user_name=? and user_password=?", getPara("user.user_name"),
				MD5Util.MD5(getPara("user.user_password")));
		if (user != null) {//获取登录用户成功
			Date nowdate = new Date();
			//每天首次登录可获取10经验
			if(isSameDay(nowdate,user.getLoginTime())){
				//同一天登录不做任何处理
			}else{//不是同一天登录 经验+10
				user.setIntegral(user.getIntegral()+10);
				//做等级统计
				if(user.getUserLevel()>1){
					if(user.getIntegral()>=(user.getUserLevel()-1)*100){
						user.setIntegral(user.getIntegral()-(user.getUserLevel()-1)*100);
						user.setUserLevel(user.getUserLevel()+1);
					}
				}else{
					if(user.getUserLevel()>0){
						if(user.getIntegral()>=50){
							user.setIntegral(user.getIntegral()-50);
							user.setUserLevel(user.getUserLevel()+1);
						}
					}else{
						if(user.getIntegral()>=10){
							user.setIntegral(user.getIntegral()-10);
							user.setUserLevel(user.getUserLevel()+1);
						}
					}
				}
			}
			user.setLoginTime(new Date());
			user.update();
			getSession().setAttribute("user", user);
			redirect("/");
		} else {//获取登录用户异常
			getSession().setAttribute("user", null);
			setAttr("errorMessage", "userException");
			forwardAction("/");
		}
	}
	
	public void view(){
		User user = User.dao.findById(getParaToInt());
		setAttr("userView",user);
	}
	
	//用户修改
	@SuppressWarnings("deprecation")
	@Before(UserValidator.class)
	public void modify(){
		getFile("picFile");
		User user = (User) getSession().getAttribute("user");	
		String imguri=getPara("head_portrait_img");
		if(user!=null&&user.getId()!=null){//已登录
			//设置修改信息
			if(imguri!=null&&!"".equals(imguri.trim())){
				String headPortraitImg = PictureBase64.getImgUrl(imguri,
						PropKit.get("uploadFileUrl")+File.separator+"head",
						getRequest().getRealPath("/"),getRequest().getContextPath());
				user.setHeadPortraitImg(headPortraitImg);
			}
			user.setNickName(getPara("nickName"));
			//user.setEmail(user2.getEmail());
			user.setMobile(getPara("tel"));
			user.setUpdateTime(new Date());
			user.update();
			redirect("/");
		}
		else{//未登录异常
			getSession().setAttribute("user", null);
			redirect("/");
		}
	}
	
	private boolean isSameDay(Date day1, Date day2) {
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String ds1 = sdf.format(day1);
	    String ds2 = sdf.format(day2);
	    if (ds1.equals(ds2)) {
	        return true;
	    } else {
	        return false;
	    }
	}
}
